package yun.web.enjoy.template.ext.sharedmethod;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

public class DefaultEngineShareMethod {
    public String md5(Object o) {
        if (o instanceof String) {
            return SecureUtil.md5((String) o);
        }
        return SecureUtil.md5(o.toString());
    }

    /*
     *  默认的aes加密
     * */
    private AES DEFAULT_AES_SECUREKEY = SecureUtil.aes(" default-engine ".getBytes());

    public String aes(Object o) {
        if (o instanceof String) {
            return DEFAULT_AES_SECUREKEY.encryptHex((String) o);
        }
        return DEFAULT_AES_SECUREKEY.encryptHex(o.toString());
    }

    /**
     * ------------------------
     * author: 张天笑
     * date: 2019/3/30
     * time: 14:05
     *
     * @param o   原始字符 串
     * @param len 从索引0开始截取需要保留的长度
     * @param b   是否拼接省略号...
     *            -------------------------
     */
    public String substring(String o, int len, boolean b) {
        if (b) len = len - 3; // 需要拼接省略号 长度减3
        if (len > o.length()) return b ? o + "..." : o;
        return b ? o.substring(0, len) + "..." : o.substring(0, len);
    }

    public String substring(String o, int len) {
        return substring(o, len, false);
    }
}
